package com.alphawallet.token.entity;

import org.w3c.dom.Element;
import org.w3c.dom.EntityReference;
import org.w3c.dom.Node;

import static org.w3c.dom.Node.ELEMENT_NODE;
import static org.w3c.dom.Node.TEXT_NODE;

/**
 * Holds an individual Token View which consists of style and HTML view code
 *
 * Created by JB on 8/05/2020.
 */
public class TSTokenView
{
    public final String tokenView;
    public final String style;

    public TSTokenView(Element element)
    {
        String lStyle = "";
        String lView = "";
        for (int i = 0; i < element.getChildNodes().getLength(); i++)
        {
            Node child = element.getChildNodes().item(i);

            switch (child.getNodeType())
            {
                case ELEMENT_NODE:
                    switch (child.getLocalName())
                    {
                        case "style":
                            //record the style for this
                            lStyle += getHTMLContent(child);
                            break;
                        default:
                            lView += getElementHTML(child);
                            break;
                    }
                    break;
                case TEXT_NODE:
                    if (element.getChildNodes().getLength() == 1)
                    {
                        //handle text item-view
                        lView = child.getTextContent().replace("\u2019", "&#x2019;");
                    }
                    break;
                default:
                    break;
            }
        }

        tokenView = lView;
        style = lStyle;
    }

    public TSTokenView(String style, String view)
    {
        this.style = style;
        this.tokenView = view;
    }

    private String getElementHTML(Node content)
    {
        StringBuilder sb = new StringBuilder();
        sb.append("<");
        sb.append(content.getLocalName());
        sb.append(htmlAttributes(content));
        sb.append(">");
        sb.append(getHTMLContent(content));
        sb.append("</");
        sb.append(content.getLocalName());
        sb.append(">");

        return sb.toString();
    }

    private String getHTMLContent(Node content)
    {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < content.getChildNodes().getLength(); i++)
        {
            Node child = content.getChildNodes().item(i);
            switch (child.getNodeType())
            {
                case ELEMENT_NODE:
                    sb.append("<");
                    sb.append(child.getLocalName());
                    sb.append(htmlAttributes(child));
                    sb.append(">");
                    sb.append(getHTMLContent(child));
                    sb.append("</");
                    sb.append(child.getLocalName());
                    sb.append(">");
                    break;
                case Node.COMMENT_NODE: //no need to record comment nodes
                    break;
                case Node.ENTITY_REFERENCE_NODE:
                    //load in external content
                    String entityRef = child.getTextContent();
                    EntityReference ref = (EntityReference) child;

                    System.out.println(entityRef);
                    break;
                default:
                    if (child != null && child.getTextContent() != null)
                    {
                        String parsed = child.getTextContent().replace("\u2019", "&#x2019;");
                        sb.append(parsed);
                    }
                    break;
            }
        }

        return sb.toString();
    }

    private String htmlAttributes(Node attribute)
    {
        StringBuilder sb = new StringBuilder();
        if (attribute.hasAttributes())
        {
            for (int i = 0; i < attribute.getAttributes().getLength(); i++)
            {
                Node node = attribute.getAttributes().item(i);
                sb.append(" ");
                sb.append(node.getLocalName());
                sb.append("=\"");
                sb.append(node.getTextContent());
                sb.append("\"");
            }
        }

        return sb.toString();
    }

    public static TSTokenView getDefaultView(String assetDetailViewName)
    {
        String style = "h3 { color: #111; font-family: 'Open Sans', sans-serif; font-size: 20px; font-weight: 300; line-height: 32px; }\n" +
                "\n" +
                "#inputBox {\n" +
                "  text-align: center;\n" +
                "}\n" +
                "\n" +
                "html,\n" +
                "body {\n" +
                "  height: 100%;\n" +
                "}\n" +
                "html {\n" +
                "  font-size: 14px;\n" +
                "}\n" +
                "body {\n" +
                "  margin: 0px;\n" +
                "  padding: 0px;\n" +
                "  overflow-x: hidden;\n" +
                "  min-width: 320px;\n" +
                "  background: #FFFFFF;\n" +
                "  font-family: 'Lato', 'Helvetica Neue', Arial, Helvetica, sans-serif;\n" +
                "  font-size: 14px;\n" +
                "  line-height: 1.4285em;\n" +
                "  color: rgba(0, 0, 0, 0.87);\n" +
                "  font-smoothing: antialiased;\n" +
                "}\n" +
                ".ui.container {\n" +
                "  display: block;\n" +
                "  max-width: 100% !important;\n" +
                "}\n" +
                "@media only screen and (max-width: 767px) {\n" +
                "  .ui.container {\n" +
                "    width: auto !important;\n" +
                "    margin-left: 1em !important;\n" +
                "    margin-right: 1em !important;\n" +
                "  }\n" +
                "}\n" +
                "@media only screen and (min-width: 768px) and (max-width: 991px) {\n" +
                "  .ui.container {\n" +
                "    width: 723px;\n" +
                "    margin-left: auto !important;\n" +
                "    margin-right: auto !important;\n" +
                "  }\n" +
                "}\n" +
                "@media only screen and (min-width: 992px) and (max-width: 1199px) {\n" +
                "  .ui.container {\n" +
                "    width: 933px;\n" +
                "    margin-left: auto !important;\n" +
                "    margin-right: auto !important;\n" +
                "  }\n" +
                "}\n" +
                "@media only screen and (min-width: 1200px) {\n" +
                "  .ui.container {\n" +
                "    width: 1127px;\n" +
                "    margin-left: auto !important;\n" +
                "    margin-right: auto !important;\n" +
                "  }\n" +
                "}\n" +
                ".ui.segment {\n" +
                "  position: relative;\n" +
                "  background: #FFFFFF;\n" +
                "  -webkit-box-shadow: 0px 1px 2px 0 rgba(34, 36, 38, 0.15);\n" +
                "  box-shadow: 0px 1px 2px 0 rgba(34, 36, 38, 0.15);\n" +
                "  margin: 0.5rem 0em;\n" +
                "  padding: 0.5em 0.5em;\n" +
                "  border-radius: 0.28571429rem;\n" +
                "  border: 1px solid rgba(34, 36, 38, 0.15);\n" +
                "  text-align: center;\n" +
                "}\n" +
                ".ui.segment:first-child {\n" +
                "  margin-top: 0em;\n" +
                "}\n" +
                ".ui.segment:last-child {\n" +
                "  margin-bottom: 0em;\n" +
                "}\n" +
                "input {\n" +
                "  position: relative;\n" +
                "  font-weight: normal;\n" +
                "  font-style: normal;\n" +
                "  font-size: 12px;\n" +
                "  display: -ms-inline-flexbox;\n" +
                "  display: inline-flex;\n" +
                "  color: rgba(0, 0, 0, 0.87);\n" +
                "  padding: 9.5px 14px;\n" +
                "  width: 300px;\n" +
                "  border-color: #D8D8D8;\n" +
                "}\n" +
                "input[type=text]:focus {\n" +
                "  border-color: #D8D8D8;\n" +
                "  background: #FAFAFA;\n" +
                "  color: rgba(0, 0, 0, 0.87);\n" +
                "  -webkit-box-shadow: none;\n" +
                "  box-shadow: none;\n" +
                "}\n" +
                "label {\n" +
                "  font-size: 12px;\n" +
                "  font-weight: 500;\n" +
                "  margin-top: 6px;\n" +
                "}";
        String view = "<script type=\"text/javascript\"> //\n" +
                "class Token {\n" +
                "\n" +
                "    constructor(tokenInstance) {\n" +
                "        this.props = tokenInstance;\n" +
                "    }\n" +
                "\n" +
                "    render() {\n" +
                "        let message = `Sent ${this.props.symbol}`;\n" +
                "        var amountFixed = (this.props.amount / 10**this.props.decimals).toFixed(2);\n" +
                "        return`\n" +
                "        &lt;div class=\"ui container\"&gt;\n" +
                "          &lt;div class=\"ui segment\"&gt;\n" +
                "            &lt;img src=\"\"&gt;\n" +
                "            &lt;span&gt;&lt;bold&gt;&lt;h3&gt;${message}&lt;/h3&gt;&lt;/bold&gt;&lt;/span&gt;\n" +
                "            &lt;p&gt;Sent ${amountFixed} ${this.props.symbol} to ${this.props.to}&lt;/p&gt;\n" +
                "          &lt;/div&gt;\n" +
                "        &lt;/div&gt;\n" +
                "`;\n" +
                "    }\n" +
                "}\n" +
                "\n" +
                "web3.tokens.dataChanged = (oldTokens, updatedTokens, tokenIdCard) =&gt; {\n" +
                "    const currentTokenInstance = web3.tokens.data.currentInstance;\n" +
                "    document.getElementById(tokenIdCard).innerHTML = new Token(currentTokenInstance).render();\n" +
                "};\n" +
                "//\n" +
                "</script>";

        return new TSTokenView(style, view);
    }
}
